/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * Licensed under Simplified BSD License
 *
 *
 * @author: jin Chen 
 * @date: Mar 8, 2005$
 * @version: 1.0
 */
package edu.psu.geovista.app.pcp.plot.control.tools;

import edu.psu.geovista.app.pcp.plot.gui.BasicPcpUI;
import edu.psu.geovista.app.pcp.plot.layer.DataLayer;
import edu.psu.geovista.app.pcp.toolkits.group.DataIDGroup;
import edu.psu.geovista.app.pcp.toolkits.group.DataIDSet;
import edu.psu.geovista.app.plot.layer.Layer;
import edu.psu.geovista.classification.ClassificationResultItf;
import edu.psu.geovista.data.model.AppDataModel;
import edu.psu.geovista.ui.event.SelectionEvent;

import java.awt.*;
import java.util.HashSet;
import java.util.Set;

public class ObsrSumDModeActor extends CategorySumDModeActor{
    private String  obSummaryVar;// 
    //DataIDGroup grp;
    public ObsrSumDModeActor(DataSelectTool datatool) {
        super(datatool);
    }

    public void fireIndicationChange(int id) {
            // 
            //this.clearSelection() ;    //wrong, select,any indicate, it clear last selection, but the selection is still on the screen.
            Integer indicateId=new Integer(id);
            //this.lastSelection.add(indicateId);
            HashSet ids = new HashSet();
            ids.add(indicateId);
            //convert summary ids to member ids and fire selection instead
            //this.getTool().fireSelectionChange(ids ) ;
    }

    public void processIndication(int id, int x, int y) {
        //do nothing
    }

    public void processSelection(SelectionEvent e) {
       //do noting
    }

    public void processClassification(ClassificationResultItf cr) {
       //do nothing
    }

    public void updateOnSwitchMode() {


        final DataSelectTool tool = this.getTool();
        BasicPcpUI ui = tool.getUI();


        edu.psu.geovista.data.model.AppDataModel dataModel=(AppDataModel) ui.getPlot().getDataModel() ;
        DataLayer srLayer=(DataLayer)ui.getLayer(Layer.TopData_LAYER );

        //common task
        tool.clearSelection();
        //tool.setStopActOnMouse(false);

        // 
        DataIDGroup grp=tool.getService().getObservationSummaryAsGroup(dataModel, this.getObSummaryVar());
        setGrpData(grp);
        //tool.seFirer.setGrpData(grp);

        AppDataModel stateModel=(edu.psu.geovista.data.model.AppDataModel) tool.getService().getObservationSummaryAsDataModel((edu.psu.geovista.data.model.AppDataModel)dataModel,grp);
        tool.updateServiceDataModel(stateModel);

        srLayer.setStrongStrokeMode(true); //in summary mode, data is paint strongly

        //generate colors for summary data.
        // 
        //  
        final int size = stateModel.getRowCount();
        Color[] colors=new Color[size];
        HashSet IDs=new HashSet(size);
        for (int i=0;i<size;i++){
            colors[i]=new Color(200,200,200);
            IDs.add(new Integer(i));
        }
        srLayer.setColors(colors);
        DataIDSet ids=new  DataIDSet(IDs,DataIDSet.TYPE_CategoryData );//here the id is catogery id, not record id
       this.getTool().showDataGroup(ids);  //will do repainting



    }

    public void setDataLayerColors(Color[] colors) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

     /*public int[] getMemberIds(DataIDGroup grpData,HashSet summaryIds) {
            int[] ids=null;

                // 
               Object[] IDs=summaryIds.toArray();
               // ArrayList grpList=grpData.getIndexSortList();
                int[] observationIds=new int[IDs.length ]; //observation id !=record id in this mode. In this case, it is actually state id
                for (int i=0;i<IDs.length ;i++){
                    int selectedRecordID=((Integer)IDs[i]).intValue() ;
                    observationIds[i]=  selectedRecordID;
                }
                Set memberIds= grpData.getIndice(observationIds);
                ids=DataIDSet.convertToIntArray(memberIds );
            return ids;
     }*/
     public int[] getMemberIds(HashSet summaryIds){
       int[] ids=null;

                // 
               Object[] IDs=summaryIds.toArray();
               // ArrayList grpList=grpData.getIndexSortList();
                int[] observationIds=new int[IDs.length ]; //observation id !=record id in this mode. In this case, it is actually state id
                for (int i=0;i<IDs.length ;i++){
                    int selectedRecordID=((Integer)IDs[i]).intValue() ;
                    observationIds[i]=  selectedRecordID;
                }
                Set memberIds= this.getGrpData().getIndice(observationIds);
                ids=DataIDSet.convertToIntArray(memberIds );
            return ids;
    }

    public String getObSummaryVar() {
        return obSummaryVar;
    }

    public void setObSummaryVar(String obSummaryVar) {
        this.obSummaryVar = obSummaryVar;
    }


}
